cmake_minimum_required (VERSION 3.22)


set(TOOLCHAIN_PATH "C:/toolchain/arm-none-eabi/bin")


# setup the toolchain path
set(ENV{PATH} "${TOOLCHAIN_PATH};$ENV{PATH}")
include("CMake/toolchain.cmake")
project (bare-metal C CXX ASM)

# set the C and C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

# set the build type
if(NOT CMAKE_BUILD_TYPE) 
    set(CMAKE_BUILD_TYPE Release)
endif(NOT CMAKE_BUILD_TYPE)
if(CMAKE_BUILD_TYPE MATCHES Debug)
  message("Debug build.")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
  message("Release build.")
endif()

# find openocd
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake/Modules)
find_package(Openocd)

#stm32f4xx_hal
set(stm32f4xx_hal_src 
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cec.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dcmi.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dcmi_ex.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dfsdm.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma2d.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dsi.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_fmpi2c.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_fmpi2c_ex.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_fmpsmbus.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_fmpsmbus_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_irda.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_iwdg.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_lptim.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc_ex.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_mmc.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_msp_template.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nand.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nor.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pccard.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_qspi.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sdram.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_smartcard.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_smbus.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spdifrx.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_timebase_rtc_alarm_template.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_timebase_rtc_wakeup_template.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_timebase_tim_template.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c
    # STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_wwdg.c
    STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c
    STM32F4xx_HAL_Driver/pure_virtual.c
    STM32F4xx_HAL_Driver/syscalls.c
    STM32F4xx_HAL_Driver/sysmem.c
    CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c
)
add_library(stm32f4xx_hal ${stm32f4xx_hal_src})
target_include_directories(stm32f4xx_hal PUBLIC 
    STM32F4xx_HAL_Driver/Inc
    CMSIS/Device/ST/STM32F4xx/Include
    CMSIS/Include
)
target_compile_definitions(stm32f4xx_hal PUBLIC 
    USE_HAL_DRIVER
    STM32F407xx
    $<$<CONFIG:Debug>:DEBUG>
    $<$<CONFIG:Debug>:USE_FULL_ASSERT>
    HSE_VALUE=8000000U
)
target_compile_options(stm32f4xx_hal PRIVATE 
    -Wno-unused-parameter
)

# add subdirectories
file(GLOB app_dirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/Applications 
     ${CMAKE_CURRENT_SOURCE_DIR}/Applications/t*-*)
foreach(dir ${app_dirs})
     if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Applications/${dir})
         add_subdirectory(Applications/${dir})
     endif()
 endforeach()
